package com.hyzh.hj212.config;

import com.coffee.concurrent.CoffeeExecutorManager;
import com.coffee.netty.CoffeeRpcServer;
import com.coffee.netty.config.ServerProperties;
import com.hyzh.hj212.netty.HjCoder;
import com.hyzh.hj212.netty.HjServerHandler;
import com.hyzh.hj212.service.LatteInitializeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author songkui
 * @since 2024/6/12
 */
@Component
@Order(1)
@Slf4j
public class StartInitialize implements ApplicationRunner {

    private final HjServerHandler hjServerHandler;
    private final LatteInitializeService latteInitializeService;

    @Value("${latte.port}")
    private Integer port;
//    @Value("${latte.host}")
//    private String host;

    @Value("${latte.heart}")
    private Long heart;

    public StartInitialize(HjServerHandler hjServerHandler, LatteInitializeService latteInitializeService) {
        this.hjServerHandler = hjServerHandler;
        this.latteInitializeService = latteInitializeService;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        start();
    }


    public void start() throws Exception {
        CoffeeExecutorManager.getInstance().disruptor(8);
        latteInitializeService.initialize();

        ServerProperties serverProperties = new ServerProperties();
        serverProperties.setPort(port);
        serverProperties.setHeartbeatSecond(heart);
//        serverProperties.setInetHost(host);

        CoffeeRpcServer rpcServer = new CoffeeRpcServer();
        rpcServer.registerRpcCodec(new HjCoder());
        rpcServer.registerCommandHandler(hjServerHandler);
        rpcServer.start(serverProperties);

    }



}
